package com.ayuan.blog.spider;

import com.alibaba.fastjson.JSON;
import com.ayuan.blog.spider.dao.BuildDao;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.model.HasKey;
import us.codecraft.webmagic.pipeline.FilePageModelPipeline;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by xiaozhu on 16/6/27.
 */
//@Component("JobInfoDaoPipeline")
public class BuildPipeline extends FilePageModelPipeline implements Pipeline{

//    @Autowired
    private BuildDao buildDao;

    private Logger logger = LoggerFactory.getLogger(getClass());

    public BuildPipeline() {
    }


    public BuildPipeline(String path) {
        super(path);
    }

    @Override
    public void process(Object o, Task task) {
        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;
        try {
            String filename;
            if (o instanceof HasKey) {
                filename = path + ((HasKey) o).key() + ".html";
            } else {
                filename = path + DigestUtils.md5Hex(ToStringBuilder.reflectionToString(o)) + ".html";
            }
            PrintWriter printWriter = new PrintWriter(new FileWriter(getFile(filename)));
            printWriter.write(ToStringBuilder.reflectionToString(o));
            for (Request request : task.getSite().getStartRequests()) {
                printWriter.write(request.getUrl());
            }
            printWriter.close();
        } catch (IOException e) {
            logger.warn("write file error", e);
        }
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        if (null != resultItems) {
            try {
                if (null != resultItems.get("buildList")) {
                    List<Build> buildList = (List<Build>) resultItems.get("buildList");
                    if (CollectionUtils.isNotEmpty(buildList)) {
                        for(Build build : buildList) {
                            buildDao.add(build);
                        }
//                        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;
//                        String filename;
//                        filename = path + resultItems.getRequest().getUrl().replaceAll("http://", "").replaceAll("/", "_") + ".html";
//                        PrintWriter printWriter = new PrintWriter(new FileWriter(getFile(filename)));
//                        printWriter.write(JSON.toJSONString(buildList));
//                        printWriter.close();
                    }
                }
            } catch (Exception e) {
                logger.warn("write file error", e);
            }
        }
    }
}
